/**
 * 时间格式化函数
 * @param {string | number} time
 * @param {string} fmt
 */
export const FormatTime = (time, fmt = 'YYYY/MM/DD hh:mm:ss') => {
  if (!time) return ''
  const date = new Date(time - 0)
  if (/(Y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, date.getFullYear() + '').substr(4 - RegExp.$1.length)
  }
  const o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  }
  for (const key in o) {
    if (new RegExp(`(${key})`).test(fmt)) {
      const str = o[key] + ''
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
    }
  }
  return fmt
}

export const weekDay = () => {
  var myDate = new Date()
  var weekend = myDate.getDay()
  var weeks = [
    '星期日',
    '星期一',
    '星期二',
    '星期三',
    '星期四',
    '星期五',
    '星期六']
  var day = weeks[weekend]
  return day
}

export const objDateChangeStr = (objDate) => {
  var date = new Date(objDate)
  var y = date.getFullYear()
  var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
  var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
  return y + '-' + m + '-' + d
}

export const strDateChangeTimeTamp = (dateStr) => {
  dateStr = dateStr.replace(/-/g, '/')
  var timeTamp = new Date(dateStr).getTime()
  return timeTamp
}

// num 天数
export function timeLag(num) {
  var newTime = new Date()
  var oldTime = num * 24 * 60 * 60 * 1000
  return {
    newtime: FormatTime(newTime, 'YYYY-MM-DD'),
    oldTime: FormatTime(newTime - oldTime, 'YYYY-MM-DD')
  }
}
